
package com.jyg.common.result;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author 橘白
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<D> {
    /**
     * 是否成功
     */
    private boolean success;
    /**
     * 错误码
     */
    private String code;
    /**
     * 错误信息
     */
    private String msg;
    /**
     * 返回数据
     */
    private D data;

    public Result(ResultEnum resultEnum) {
        this.success = resultEnum.isSuccess();
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }

    public Result(ResultEnum resultEnum, D data) {
        this.success = resultEnum.isSuccess();
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
        this.data = data;
    }

    /**
     * 无参正常返回
     *
     * @return
     */
    public static Result<?> ok() {
        return new Result<>(ResultEnum.SUCCESS);
    }

    /**
     * 有参正常返回
     *
     * @param data
     * @return
     */
    public static Result<?> ok(Object data) {
        Result<Object> result = new Result<>(ResultEnum.SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 无参异常
     *
     * @return
     */
    public static Result<?> err() {
        return new Result<>(ResultEnum.FAILED);
    }

    /**
     * 提示类型的异常
     *
     * @param msg
     * @return
     */
    public static Result<?> hint(String msg) {
        Result<?> result = new Result<>(ResultEnum.HINT);
        result.setMsg(msg);
        return result;
    }

    /**
     * 打印debug日志的异常
     *
     * @param msg
     * @return
     */
    public static Result<?> debug(String msg) {
        Result<?> result = new Result<>(ResultEnum.DEBUG);
        result.setMsg(msg);
        return result;
    }
}
